﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HAUtil;

namespace HA.Data
{
    /// <summary>
    /// 概念模型中的节点信息 非线中直接使用
    /// </summary>
    [Serializable]
    public class NodeFO:FeatureObject
    {
        public NodeFO(string id, HCoordinate coordinate, Coverage coverage )
            : base(id,coverage)
        {
            if (coordinate == null)
                throw new Exception("点坐标不能为null！");
            _position = coordinate;

            _relationLines = new List<LineFO>();
        }

        public NodeFO(string id, HCoordinate coordinate, List<LineFO> relationLines, Coverage coverage)
            : base(id,coverage)
        {
            if (coordinate == null)
                throw new Exception("点坐标不能为null！");
            _position = coordinate;

            if(relationLines==null||relationLines.Count==0)
                throw new Exception("点关系不能为null或空！");
            _relationLines = relationLines;
        }

        HCoordinate _position;

        public HCoordinate Position
        {
            get { return _position; }
            set
            {
                if (value == null)
                    throw new Exception("点坐标不能为null!");

                _position = value;
                for (int i = 0; i < _relationLines.Count; i++)
                {
                    //_relationLines[i].
                }

            }
        }

        List<LineFO> _relationLines;

        public List<LineFO> RelationLines
        {
            get { return _relationLines; }
            set { _relationLines = value; }
        }

        bool _isVisited = false;
        /// <summary>
        /// 遍历标志位
        /// </summary>
        public bool IsVisited
        {
            get { return _isVisited; }
            set { _isVisited = value; }
        }

        /// <summary>
        /// 该函数进行的是端点更新时需要的 额外 操作
        /// </summary>
        public void UpdateForVertexChanged(CollectionChangedType type,ref List<string> ids)
        {
            switch (type)
            { 
                case CollectionChangedType.REMOVE : //删除

                    ///分情况 占位待添加
                    if (this._relationLines.Count == 1)
                    {

                    }
                    else if (this._relationLines.Count == 2)
                    {

                    }
                    else
                    {

                    }

                    //全删逻辑
                    for (int i = this._relationLines.Count - 1; i >= 0; i = _relationLines.Count - 1)
                    {

                        //删除点时，对应的线可能是删除，也可能是编辑
                        //确定后再添代码

                        //先用简单的方法删除
                        //this.Parent.Remove(line);
                        if (this._relationLines[i].VertexStart.Id != this.Id && this._relationLines[i].VertexEnd.Id != this.Id)
                            throw new Exception("线的两端点信息错误！");

                        if (!ids.Contains(this._relationLines[i].Id))
                        {
                            ids.Add(this._relationLines[i].Id);
                            this._relationLines[i].UpdateForLineChanged(CollectionChangedType.REMOVE, ref ids);
                        }
                    }

                    break;
                case CollectionChangedType.MOVE:

                    foreach (LineFO line in this.RelationLines)
                    {
                        if (line.VertexStart.Id != this.Id && line.VertexEnd.Id != this.Id)
                        {
                            throw new Exception("线的两端点信息错误！");
                        }
                        if (line.VertexStart.Id == this.Id)
                            line.VertexStart.Node = this;
                        if (line.VertexEnd.Id == this.Id)
                            line.VertexEnd.Node = this;
                        

                        if (!ids.Contains(line.Id))
                            ids.Add(line.Id);

                        line.UpdateForLineChanged(CollectionChangedType.MOVE,ref ids);
                    }

                    break;
                default :
                    break;
            }

            //以下原代码，刘注销。

            //foreach (LineFO item in _relationLines)
            //{
            //    if (item.VertexStart.Id == this.Id)
            //    {
            //        item.VertexStart.Node = this;
            //    }
            //    else if (item.VertexEnd.Id == this.Id)
            //    {
            //        item.VertexEnd.Node = this;
            //    }
            //    else
            //        throw new Exception("存储信息有错！");

            //    item.UpdateForLineChanged();
            //}
        }

        public override object Clone()
        {
            NodeFO node = (NodeFO)base.Clone();

            node._position = (HCoordinate)_position.Clone();
            node._relationLines = new List<LineFO>();
            node._relationLines.AddRange(_relationLines);

            return node;
        }

    }
}
